package com.bw.property.view

import android.util.Log
import android.view.View
import androidx.fragment.app.activityViewModels
import androidx.recyclerview.widget.GridLayoutManager
import com.alibaba.android.arouter.launcher.ARouter
import com.blankj.utilcode.util.GsonUtils
import com.bw.common.mvi.BaseMviFragment
import com.bw.common.path.RouthPath
import com.bw.property.R
import com.bw.property.adapter.ParkListAdapter
import com.bw.property.databinding.FragmentParkApplyBinding
import com.bw.property.entity.ParkValue
import com.bw.property.intent.ParkViewIntent
import com.bw.property.state.ParkUiState
import com.bw.property.vm.ParkViewModel
import dagger.hilt.android.AndroidEntryPoint

@AndroidEntryPoint
class ParkApplyFragment : BaseMviFragment<FragmentParkApplyBinding>() {
    private val mViewModel:ParkViewModel by activityViewModels()
    private var mParkList = mutableListOf<ParkValue>()

    private lateinit var mParkListAdapter: ParkListAdapter

    companion object {
       @JvmStatic
       fun newInstance() = ParkApplyFragment()
    }

    override fun initView() {
        //布局管理器,决定布局样式
        mDataBind.rvApplyList.layoutManager = GridLayoutManager(requireContext(),6,GridLayoutManager.VERTICAL,false)
        //设置数据适配器
        mParkListAdapter = ParkListAdapter(mParkList)
        mParkListAdapter.setOnItemClickListener(object :ParkListAdapter.ItemParkOnClickListener{
            override fun onClick(v: View, position: Int) {
                var entity = mParkList[position]
                if(entity.parkUser > 0){
                    ARouter.getInstance().build(RouthPath.PATH_PARK_DETAIL_PAGE).withSerializable("parkEntity",entity).navigation()
                }else{
                    showToastMsg("当前车位可以被申请")
                }
            }

        })
        mDataBind.rvApplyList.adapter = mParkListAdapter


    }

    override suspend fun sendViewIntent() {
        super.sendViewIntent()
        mViewModel.channel.send(ParkViewIntent.LoadParkList)
    }

    override suspend fun collectUiState() {
        super.collectUiState()
        mViewModel.uiState.collect{
            when(it){
                is ParkUiState.LoadParkListSuccess ->showParkList(it.data as List<ParkValue>)
                is ParkUiState.LoadParkListFail ->showToastMsg(it.msg)
                else -> {}
            }
        }

    }

    private fun showParkList(list: List<ParkValue>) {
        if(list.isNotEmpty()){
            Log.i("=====", "车位列表返回：${GsonUtils.toJson(list)}")
            var newParkList = mutableListOf<ParkValue>()
            //计算一共有几行
            var rowNum = if(list.size%5>0) list.size/5+1 else list.size/5
            var lastIndex = 0
            for (index in 1 .. rowNum){
                newParkList.add(ParkValue().apply {
                    parkUser = -1
                    parkName = "第${index}排"
                })
                lastIndex = if(index*5>list.size) list.size else index * 5
                newParkList.addAll(list.subList((index-1)*5,lastIndex))
            }

            mParkList.addAll(list)
            mParkListAdapter.notifyDataSetChanged()
        }else{
            showToastMsg("当前没哟车位消息")
        }
    }

    override fun getLayoutId(): Int =R.layout.fragment_park_apply
}